package uno;
import java.util.ArrayList;
import java.util.Scanner;

public class Player {
        public ArrayList<Card> hand = new ArrayList<Card>();
        public CardGenerator generator = new CardGenerator();
        Scanner keyboard = new Scanner(System.in);

        Player(int InitCards) {
                CardGenerator generator = new CardGenerator();
                for (int x = 0; x < InitCards; x++) {
                        hand.add(generator.getCard());
                }
        }

        public Card playerTurn(Card card) {
                Card ReturnCard = null;
                int finished = 0;
                
                Testing test = new Testing();
                //test.printHand(hand);
                
                //Attempt to play a normal colored card
                for (int x = 0; x < hand.size(); x++) {
                        if ((hand.get(x).CardColor == card.CardColor || hand.get(x).CardNumber == card.CardNumber) && finished == 0 && hand.get(x).CardNumber != 13 && hand.get(x).CardNumber != 14) {
                                ReturnCard = hand.get(x);
                                hand.remove(x);
                                finished++;
                                //System.out.println("Played "+x);
                        }
                }
                
                //Attempt to play a uncolored card
                if(finished == 0){
                        for (int x = 0; x < hand.size(); x++) {
                                if (finished == 0 && (hand.get(x).CardNumber == 13 || hand.get(x).CardNumber == 14)) {
                                        ReturnCard = hand.get(x);
                                        hand.remove(x);
                                        finished++;
                                        ReturnCard.CardColor = bestColor();
                                        //System.out.println("Played "+x+". And chose "+ReturnCard.CardColor);
                                }
                        }
                }
                
                //Draw until you can play
                while (finished == 0) {
                        Card randCard = generator.getCard();
                        //System.out.println("Drew a card. Got a "+test.printCard(randCard)+".");
                        if (randCard.CardColor == card.CardColor || randCard.CardNumber == card.CardNumber) {
                                ReturnCard = randCard;
                                //System.out.println("Played it.");
//                              try {
//                                      System.in.read();
//                              } catch (IOException e) {
//                                      
//                                      e.printStackTrace();
//                              }
                                finished++;
                        } else if(randCard.CardNumber==13 || randCard.CardNumber == 14){
                                ReturnCard = randCard;
                                //System.out.println("Played it.");
                                ReturnCard.CardColor = bestColor();
                                finished++;
                        }else{
                                hand.add(randCard);
                                //System.out.print("Added it to hand");
//                              try {
//                                      System.in.read();
//                              } catch (IOException e) {
//                                      
//                                      e.printStackTrace();
//                              }
                        }
                }
                if(hand.size() == 0){
                        ReturnCard.CardColor = -2;
                }
                return ReturnCard;
        }

        public void PlayerDraw(int number) {
                for (int x = 0; x < number; x++) {
                        hand.add(generator.getCard());
                }
        }
        
        public int bestColor(){
                int[] count = new int[4];
                for(int x = 0; x < hand.size(); x++){
                        if(hand.get(x).CardColor != -1){
                                count[hand.get(x).CardColor]++;
                        }       
                }
                int currentHighest = -1;
                int currentHeighestNumber = -1;
                for(int x = 0; x < 4; x++){
                        if(currentHeighestNumber < count[x]){
                                currentHighest = x;
                                currentHeighestNumber = count[x];
                        }
                }
                return currentHighest;
        }
}